package com.encryption.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.encryption.dao.Condition;
import com.encryption.dao.RoleDAO;
import com.encryption.model.Role;
import com.encryption.service.RoleService;
/**
 * 角色接口实现类
 * @author Administrator
 *
 */
public class RoleServiceImpl implements RoleService {
	
	private static final Logger log = LoggerFactory.getLogger(RoleServiceImpl.class);
	private static RoleServiceImpl roleServiceImpl = null;
	
	
	public static  RoleServiceImpl getRoleServiceImpl(){
		if(null == roleServiceImpl) {
			roleServiceImpl = new RoleServiceImpl();
		} 
		return roleServiceImpl;
	}
	/**
	 * 添加角色
	 * @param role
	 */
	public boolean addRole(Role role){
		try {
			RoleDAO.getRoleDAO().save(role);
		} catch (Exception e) {
			log.error("添加角色异常",e);
			return false;
		} 
		return true;
	}
	
	/**
	 * 编辑角色
	 * @param role
	 * @return
	 */
	public boolean updateRole(Role role){
		try {
			RoleDAO.getRoleDAO().merge(role);
		} catch (Exception e) {
			log.error("编辑角色异常",e);
			return false;
		} 
		return true;
	}
	
	
	/**
	 * 删除角色
	 * @param id
	 */
	public boolean deleteRole(String roleId){
		try {
			RoleDAO.getRoleDAO().
			delete(RoleDAO.getRoleDAO().findById(Integer.parseInt(roleId)));
		} catch (Exception e) {
			log.error("删除角色异常",e);
			return false;
		}
		return true;	}
	
	/**
	 * 查询角色列表
	 * @return
	 */
	public List<Role> getRoleByCondition(List<Condition> conditions){
		return RoleDAO.getRoleDAO().findByExample(conditions);
	}
	
	/**
	 * 分页查询角色列表
	 * @param instance
	 * @param page
	 * @return
	 */
	public List<Role> getRolePagingByCondition(List<Condition> condList, int page, int pageSize){
		return RoleDAO.getRoleDAO().findByExample4Page(condList,page,pageSize);
	}
	
	/**
	 * 获取单个角色,根据Roleid
	 * @param RoleId
	 * @return
	 */
	public Role getRileById(String roleId){
		return  RoleDAO.getRoleDAO().findById(Integer.parseInt(roleId));
	}
	
	/**
	 * 获取单个角色,根据角色名称
	 * @param RoleId
	 * @return
	 */
	public Role getRoleByRoleName(String roleName){
		return  (Role)(RoleDAO.getRoleDAO().findByName(roleName).get(0));
	}
	 
}
